package ${actionPack};

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import ${bizPack}.${className}Biz;
import ${entityPack}.${className};
import com.sinosoft.system.util.Result;
import com.sinosoft.system.util.page.Pagenation;

/*
 * 
 * GET 方式请求 /${className}/selectAll 			返回数据列表
 * POST 方式请求 /${className}/selectInfo 			根据查询条件返回数据列表
 * GET 方式请求 /${className}/select/{id}	  			查询指定ID的数据，返回一条数据
 * POST方式请求 /${className}/select		 			分页查询数据列表
 * POST方式请求 /${className}/insert               	新增一条数据
 * POST方式请求 /${className}/update		          	更新指定ID的数据
 * GET 方式请求 /${className}/delete/{id}          	删除指定ID的数据
 * GET 方式请求 /${className}/delete  				 删除所以数据
 * 
 * @author shangxp
 * @date 2017-1-8
 * 
 */


@RestController
@RequestMapping("${className}") 
public class ${className}Action {

	private static Logger log = Logger.getLogger(${className}Action.class);
	
	@Autowired
	private ${className}Biz ${instName}Biz;

	@RequestMapping(value = "/selectAll", method = RequestMethod.POST,produces = "application/json;charset=UTF-8") 
	@CrossOrigin(methods={RequestMethod.GET}) 
    public ResponseEntity<List<${className}>> selectAll${className}() { 
     
		log.info("查询所有的${className}数据");
        List<${className}> ${instName}s = ${instName}Biz.selectAll${className}();
        if(${instName}s.isEmpty()){  
            return new ResponseEntity<List<${className}>>(HttpStatus.NO_CONTENT);//You many decide to return HttpStatus.NOT_FOUND  
        }  
        return new ResponseEntity<List<${className}>>(${instName}s, HttpStatus.OK);  
    }
    
    @RequestMapping(value = "/selectInfo", method = RequestMethod.POST,produces = "application/json;charset=UTF-8") 
	@CrossOrigin(methods={RequestMethod.POST}) 
    public ResponseEntity<List<${className}>> selectInfo${className}(HttpServletRequest request) { 
     	log.info("根据条件查询所有的${className}数据");
		List<String> queryKeys = new ArrayList<String>();
		List<Object> queryValues = new ArrayList<Object>();
		//添加查询条件   根据查询情况修改
/*		String username = request.getParameter("username");
		if(username != null && !"".equals(username.trim())){
			queryKeys.add("username"); //querykey 对应 数据库 字段名，dao查询时拼SQL使用
			queryValues.add(username); //queryValue  对应查询值
		}
*/		
		List<${className}> ${instName}List = ${instName}Biz.selectAll${className}(queryKeys,queryValues);
		
        if(${instName}List==null||${instName}List.isEmpty()){  
            return new ResponseEntity<List<${className}>>(HttpStatus.NO_CONTENT);//You many decide to return HttpStatus.NOT_FOUND  
        }  
        return new ResponseEntity<List<${className}>>(${instName}List, HttpStatus.OK);
    }
	
	@RequestMapping(value = "/select/{id}", method = RequestMethod.GET,produces = "application/json;charset=UTF-8") 
	@CrossOrigin(methods={RequestMethod.GET}) 
    public ResponseEntity<${className}> select${className}ById(@PathVariable("id") int id) {  
		log.info("查询ID="+id+"的${className}数据");
        ${className} ${instName} = ${instName}Biz.select${className}(id);
        if(${instName}==null){  
            return new ResponseEntity<${className}>(HttpStatus.NO_CONTENT);//You many decide to return HttpStatus.NOT_FOUND  
        }  
        return new ResponseEntity<${className}>(${instName}, HttpStatus.OK);  
    }
	
	@RequestMapping(value = "/select", method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
	@CrossOrigin(methods={RequestMethod.POST})   
    public ResponseEntity<Pagenation> selectPage${className}(HttpServletRequest request) {  
		log.info("分页查询${className}数据");
		//  分页查询开始
		int pageNumber = new Integer(request.getParameter("page"));//easyui传入的是page是页码
		int pageSize = new Integer(request.getParameter("rows"));//easyui传入的rows是页大小
		List<String> queryKeys = new ArrayList<String>();
		List<Object> queryValues = new ArrayList<Object>();
		//添加查询条件   根据查询情况修改
/*		String username = request.getParameter("username");
		if(username != null && !"".equals(username.trim())){
			queryKeys.add("username"); //querykey 对应 数据库 字段名，dao查询时拼SQL使用
			queryValues.add(username); //queryValue  对应查询值
		}
*/		
		Pagenation page = new Pagenation(pageSize,pageNumber,queryKeys,queryValues);
		page = ${instName}Biz.selectPage${className}(page);
		
        if(page==null){  
            return new ResponseEntity<Pagenation>(HttpStatus.NO_CONTENT);//You many decide to return HttpStatus.NOT_FOUND  
        }  
        return new ResponseEntity<Pagenation>(page, HttpStatus.OK);  
    }
	
	@RequestMapping(value = "/insert", method = RequestMethod.POST,produces = "application/json;charset=UTF-8") 
	@CrossOrigin(methods={RequestMethod.POST}) 
    public ResponseEntity<Result> insert${className}(${className} ${instName}) {
		log.info("插入一条${className}数据："+${instName}.toString());
		
		if(${instName}Biz.insert${className}(${instName})!=1){
			return new ResponseEntity<Result>(new Result("insertError","新增数据失败"), HttpStatus.OK); 
		}else{
			return new ResponseEntity<Result>(new Result("insertOk","新增数据成功"), HttpStatus.OK);
		}
    }
	
	@RequestMapping(value = "/update", method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
	@CrossOrigin(methods={RequestMethod.POST})  
    public ResponseEntity<Result> update${className}(${className} ${instName}) {  
		log.info("更新一条${className}数据："+${instName}.toString());		
		if(${instName}Biz.update${className}(${instName})!=1){
			return new ResponseEntity<Result>(new Result("updateError","更新数据失败"), HttpStatus.OK); 
		}else{
			return new ResponseEntity<Result>(new Result("updateOk","更新数据成功"), HttpStatus.OK);   
		}
    }
	
	@RequestMapping(value = "/delete/{id}", method = RequestMethod.GET,produces = "application/json;charset=UTF-8")
	@CrossOrigin(methods={RequestMethod.GET})  
    public ResponseEntity<Result> delete${className}ById(@PathVariable("id") int id) { 
		log.info("删除ID="+id+"的${className}数据");
		if(${instName}Biz.delete${className}(id)!=1){
			return new ResponseEntity<Result>(new Result("deleteError","删除数据失败"), HttpStatus.OK); 
		}else{
			return new ResponseEntity<Result>(new Result("deleteOk","删除数据成功"), HttpStatus.OK);   
		}
    }
	
	@RequestMapping(value = "/delete", method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
	@CrossOrigin(methods={RequestMethod.POST})  
    public ResponseEntity<String> delete${className}() {  
		log.info("删除所有的${className}数据");		
		//为实现
        return new ResponseEntity<String>("OK", HttpStatus.OK);  
    }
	

	public ${className}Biz get${className}Biz() {
		return ${instName}Biz;
	}

	public void set${className}Biz(${className}Biz ${instName}Biz) {
		this.${instName}Biz = ${instName}Biz;
	}

}
